<?php
namespace App\Http\Controllers\Api\V4;

use App\Http\Controllers\Api\V4\BaseController;
use App\Models\V2\MemberModel;
use DB;
use Illuminate\Http\Request;
use App\Common\ApiData;
use App\Repository\Comment;

class CommentController extends BaseController{

    private $apiData;
    use Comment;

    public function __construct()
    {
        $this->apiData  = new ApiData();
    }

    /**
     * 添加评论
     * */
    public function add_comment(Request $request)
    {
        $mobile         = $request->input('mobile');
        $key            = $request->input('key');
        $contentId      = $request->input('contentId');
        $parentCommentId= $request->input('parentCommentId',0);
        $topCommentId   = $request->input('topCommentId',0);
        $commentContent = $request->input('commentContent');
        $commentContent = emoji_text_encode($commentContent);
        $type           = $request->input('type');

        $params = [
            'mobile'    => [$mobile,'缺少手机号',3001],
            'key'       => [$key,'缺少设备ID',3002],
            'id'        => [$contentId,'缺少主体ID',3003],
            'content'   => [$commentContent,'评论内容为空',3004],
            'type'      => [$type,'缺少类型ID',3005]
        ];

        $checkResult    = $this->check_params($params);
        if(!$checkResult->status)
        {
            return $this->apiData->send($checkResult->code,$checkResult->message);
        }


        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile_key($mobile,$key);
        if(!$memberInfo)
        {
            return $this->apiData->send(4001,'用户不存在');
        }

        if($parentCommentId > 0)
        {
            $parentComment  = DB::table('comments')->where('id',$parentCommentId)->first();
            $replayUserid   = $parentComment->user_id;
        }
        else
        {
            $replayUserid   = 0;
        }
        $date   = date_time();
        $data   = [
            'user_id'       => $memberInfo->UserID,
            'pid'           => $parentCommentId,
            'replay_user_id'=> $replayUserid,
            'body'          => $commentContent,
            'lessons_id'    => $contentId,
            'type'          => $type,
            'created_at'    => $date,
            'updated_at'    => $date
        ];

        $id = DB::table('comments')->insertGetId($data);

        $commentId  = $topCommentId > 0 ? $topCommentId : $id;
        $comment    = $this->get_comment($commentId);
        return $this->apiData
            ->set_data('data',$comment)
            ->send_old(200,'ok');
    }




    /**
     * 获得
     * */
    public function get_single_comment(Request $request)
    {
        $commentId  = $request->input('commentId');
        $comment    = $this->get_comment($commentId);

        return $this->apiData
            ->set_data('data',$comment)
            ->send_old(200,'ok');
    }


    /**
     * 删除评论信息
     * */
    public function delete_comment(Request $request)
    {
        $commentId  = $request->input('commentId');
        $result = DB::table('comments')
            ->where('id',$commentId)
            ->update(['deleted_at'=>date_time()]);
        return $this->apiData->send(200,'ok');
    }


    /**
     * 给评论点赞
     * */
    public function add_or_cancel_zan(Request $request)
    {
        $mobile     = $request->input('mobile');
        $key        = $request->input('key');
        $commentId  = $request->input('commentId');

        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile_key($mobile,$key);
        $userId         = $memberInfo->UserID;
        $commentInfo    = DB::table('comments')->where('id',$commentId)->first();
        $users          = explode(',',$commentInfo->up_time_user);
        $key            = array_search($userId,$users);
        if($key === false) //未点赞
        {
            array_push($users,$userId);
            $times  = $commentInfo->up_times + 1;
        }
        else //已点赞
        {
            unset($users[$key]);
            $times  = $commentInfo->up_times - 1;
        }
        $users  = implode(',',$users);
        DB::table('comments')->where('id',$commentId)->update(['up_time_user'=>$users,'up_times'=>$times]);
        return $this->apiData->send(200,'ok');
    }
}